//
//  AttachedPictureView.m
//  ABlip
//
//  Created by dawid on 09-01-19.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AttachedPictureView.h"
#import "MessageView.h"
#import "ABlipConstants.h"
#define FONT_COLOR  0.3176
@implementation AttachedPictureView
@synthesize pictureName;
@synthesize imageData;
@synthesize textAttributes;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
		textAttributes = [[NSMutableDictionary alloc] init];
		[textAttributes setObject:[NSFont fontWithName:@"Helvetica" size:11] forKey:NSFontAttributeName];
		[textAttributes setObject:[NSColor colorWithCalibratedRed:FONT_COLOR green:FONT_COLOR blue:FONT_COLOR alpha:1] forKey:NSForegroundColorAttributeName];
    }
	[[self window] setAcceptsMouseMovedEvents:YES];
    return self;
}
-(void)dealloc
{
	if (textAttributes){
		[textAttributes release];
		textAttributes = nil;
	}
	if (pictureName){
		[pictureName release];
		pictureName = nil;
	}
	if (imageData){
		[imageData release];
		imageData = nil;
	}
	[super dealloc];
}
- (void)drawRect:(NSRect)rect {
    // Drawing code here.
	NSRect replyRect;
	replyRect.origin = NSZeroPoint;
	replyRect.size = [AttachedPictureView background].size;
	[[AttachedPictureView background] drawInRect:[self bounds] fromRect:replyRect operation:NSCompositeSourceOver fraction:1];
	
	replyRect.origin = NSZeroPoint;
	replyRect.size = NSMakeSize(13, 13);
	//replyImageRect = NSMakeRect(bounds.size.width-15,2 , replyRect.size.width,replyRect.size.height );

	//[[MessageView deleteButton] drawInRect:NSMakeRect([self bounds].size.width - 15, 8, 13, 13) fromRect:replyRect operation:NSCompositeSourceOver fraction:1];

	[self drawStringInRect:pictureName inRect:NSMakeRect(15, 8, 55, 13)];
}

-(void)drawStringInRect:(NSString*)string1 inRect:(NSRect)r
{

	if ([string1 length] > 9){
		string1 = [string1 substringToIndex:9];
		string1 = [NSString stringWithFormat:@"%@...", string1];
	}
	[string1 drawInRect:r withAttributes:[self textAttributes]];
}
-(void)setPictureName:(NSString*)value
{
	[value retain];
	[pictureName release];
	pictureName = value;
	[self setNeedsDisplay:YES];
}
#pragma mark mouse_events
-(void)viewDidMoveToWindow
{
	int options = NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways;
	NSTrackingArea *ta;
	//NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:ABL_SUBJECT_REPLY, ABL_SUBJECT, nil];
	ta = [[NSTrackingArea alloc] initWithRect:NSMakeRect([self bounds].size.width - 15, 8, 13, 13) options:options owner:self userInfo:nil];
	[self addTrackingArea:ta];
	[ta release];
	ta = nil;
	
}
-(void)mouseDown:(NSEvent*)event 
{
	NSPoint p = [event locationInWindow];
	p = [self convertPoint:p fromView:nil];
	if (NSPointInRect(p, NSMakeRect([self bounds].size.width - 15, 8, 13, 13)) == YES){
		NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
		[nc postNotificationName:ABL_PictureAttDeleteNotification
						  object:self
						userInfo:nil];
#ifdef __DEBUG__

		NSLog(@"click delete");

#endif
	}
}

-(void)mouseEntered:(NSEvent*)theEvent
{
	
	//[self setDeleteIconRollover:YES];
	[self setNeedsDisplay:YES];
}
-(void)mouseExited:(NSEvent*)theEvent
{
	//[self setDeleteIconRollover:NO];
	[self setNeedsDisplay:YES];
}
+(NSRect)deleteIconRect
{
	return NSMakeRect(75, 0, 13, 13);
}
+(NSImage*)background
{
	static NSImage* image = nil;
	if (image==nil){
		image = [NSImage imageNamed:@"filePlacement"];
	}
	return image;
}
@end
